import { Component, OnInit, ViewChild } from '@angular/core';
import { NavHeaderService } from '@global/layout/default/nav-header/nav-header.service';
import { TriggerTableComponent } from "../../../shared-component/trigger-table/trigger-table.component";
import { FcsService } from '../../../service/fcs.service';
import { CommonService } from '../../../service/common.service';
import { Environment } from "environments/environment";

@Component({
  selector: 'app-trigger-list',
  templateUrl: './trigger-list.component.html',
  styleUrls: ['./trigger-list.component.less']
})
export class TriggerListComponent implements OnInit {
  @ViewChild('triggerTable', {static: true}) triggerTable: TriggerTableComponent;
  isDetail = false;
  groupId = '0';
  applicationList = [];
  isLoadingApplication = false;
  selectGroupDisplayName = '';
  functionList = [];
  functionId = '0';
  isLoadingFunction = false;
  disableFunctionOption = false;
  triggerTypeList:Array<any> = Environment.application.triggerTypeList;
  isLoading = true;
  totalCount = 0;
  triggerType = 'all';
  triggerName = '';
  currentRegion = {
    regionCode:'',
    regionName:''
  };
  constructor(private fcsService: FcsService,
   private commonService: CommonService,
   private navHeaderService: NavHeaderService) { }

  ngOnInit() {
    this.getApplicationList();
    this.getCurrentRegion();
  }

  getCurrentRegion(){
    this.commonService.getRegions().subscribe(res => {
      if(res && res['data']){
         let regions = res['data'];
         let regionId = this.navHeaderService.getCurrentRegion();
         let regionName = this.commonService.getRegionName(regions,regionId);
         this.currentRegion['regionCode'] = regionId;
         this.currentRegion['regionName'] = regionName;
      }
    })
  }

  getApplicationList(){
    this.fcsService.getGroupList().subscribe(res => {
      this.applicationList = res || [];
      this.applicationList.unshift({id:'0',displayName:'全部应用'})
      this.getFunctionList(this.groupId);
    })
  }

  getFunctionList(groupId){
    if (groupId != '' && groupId !='0') {
      this.selectGroupDisplayName = this.applicationList.filter(item => item.id === this.groupId)[0].displayName;
      this.triggerTable.groupDisplayName = this.selectGroupDisplayName;
      this.isLoadingFunction = true;
      this.fcsService.getFuctionList(groupId, '', '', '').subscribe(res => {
        this.functionList = res.result || [];
        this.functionList.unshift({"functionId":'0',functionName:'全部函数'});
        this.functionId = this.functionList.length > 0 ? this.functionList[0].functionId : '0';
        this.isLoadingFunction = false;
        this.triggerTypeChange();
      }, error => {
        //this.errorMsg = error;
        this.isLoadingFunction = false;
      })
    }else {
      this.triggerTable.groupDisplayName = '';
      this.isLoadingFunction = true;
      this.fcsService.getAllFunctions(1,10000,'').subscribe(res => {
        this.functionList = res.result || [];
        this.functionList.unshift({"functionId":'0',functionName:'全部函数'});
        this.functionId = this.functionList.length > 0 ? this.functionList[0].functionId : '0';
        this.isLoadingFunction = false;
        this.triggerTypeChange();
      }, error => {
        //this.errorMsg = error;
        this.isLoadingFunction = false;
      })
    }
  }

  functionIdChange(){
    if(this.functionId && this.functionId != '0'){
      this.groupId = this.functionList.filter(item => item.functionId === this.functionId)[0].groupId;
    }
    this.triggerTypeChange();
  }

  triggerTypeChange(){
    this.triggerTable.type = this.triggerType;
    this.triggerTable.getTriggerList(this.groupId, this.functionId);
  }

  createTrigger() {
    this.triggerTable.groupId = this.groupId;
    this.triggerTable.functionId = this.functionId;
    this.triggerTable.type = this.triggerType;
    this.triggerTable.createTrigger();
  }

  resetTriggerTypeList(event){
    if(event && event['triggerTypes']){
      this.triggerTypeList = event['triggerTypes'];
    }
  }

  searchTriggers(){
    this.triggerTable.triggerName = this.triggerName;
    this.triggerTable.getTriggerList(this.groupId, this.functionId);
  }

  refreshTriggerList() {
    // this.groupId = '0';
    // this.functionId = '0';
    // this.triggerType = 'all';
    // this.triggerName = "";
    // this.triggerTable.triggerName = this.triggerName;
    this.triggerTable.getTriggerList(this.groupId, this.functionId);
  }
}
